<?php
/**
* @author Oliver Leisalu, Inditel Meedia
* @package DomFg
* @subpackage Input
*/
class DomFgTree extends DomFgInput {
	
	protected $source;
	
	public function __construct( $name = NULL ) {
		parent::__construct( $name );
	}

	public function source( $source ) {
		if( !isset($source[0]) && !empty($source) ) 
			$this->source = self::makeTree( $source );
		else
			$this->source = $source;
		return $this;
	}

	public static function parsePost( $name, &$value ) {
		
	}
		
	private static function makeTree2( array &$config, $parentId = NULL, $level = 0 ) {
		
		$defaultConfig = array(
			'rootParentId' => NULL,
			'idColumn' => 'id',
			'parentIdColumn' => 'parent',
			'nameColumn' => 'name',
			'callback' => false,
			'callbackArgs' => array(),
			'includeRootParent' => true,
		);
		$config = arrayMerge( $defaultConfig, $config );
		
		$list = array();
		if( $level == 0 && $config['includeRootParent'] === true && $config['rootParentId'] > 0 ) {
			$useQuery = clone $config['query'];
			$element = $useQuery->where(''.$config['idColumn'].' = '.(int)$config['rootParentId'] )->limit(1)->commit();
			
			$rootNode = DomFgTreeNode::c('');

			if( $config['callback'] ) {
				call_user_func_array($config['callback'], array_merge(array($node,$element),$config['callbackArgs']) );
			} else {
				if( is_object($element) ) {
					$rootNode->name($element->{$config['nameColumn']});
				} else {
					$rootNode->name($element[$config['nameColumn']]);
				}
			}			
		}
		
		$useQuery = clone $config['query'];
		$useQuery->where(''.$config['parentIdColumn'].' '.($parentId == NULL ? ($config['rootParentId']>0 ? ' = '.(int)$config['rootParentId'] : ' <=> NULL') : ' = '.(int)$parentId) );
		foreach( $useQuery->commit() as $element ) {
			
			$node = DomFgTreeNode::c('');
			
			// add parents to node
			$parentId = is_object($element) ? $element->{$config['idColumn']} : $element[$config['idColumn']];
			$node->set( self::makeTree( $config, $parentId, $level+1 ));
			
			if( $config['callback'] ) {
				call_user_func_array($config['callback'], array_merge(array($node,$element),$config['callbackArgs']) );
			} else {
				if( is_object($element) ) {
					$node->name($element->{$config['nameColumn']});
				} else {
					$node->name($element[$config['nameColumn']]);
				}
			}
			
			$list[] = $node;
		}
		
		if( $rootNode ) {
			$rootNode->set( $list );
			return array($rootNode);
		} else
			return $list;
	}
	
	public function makeTree( array &$config, $element = NULL, $level = 0 ) {
		
		$defaultConfig = array(
			'rootParentId' => NULL,
			'idColumn' => 'id',
			'parentIdColumn' => 'parent',
			'nameColumn' => 'name',
			'callback' => false,
			'callbackArgs' => array(),
			'includeRootParent' => true,
		);
		$config = arrayMerge( $defaultConfig, $config );
		
		
		
		// add first element if no element added and configed properly
		if( $element === NULL && $config['includeRootParent'] === true && $config['rootParentId'] > 0) {
			$useQuery = clone $config['query'];
			$element = $useQuery->where(''.$config['idColumn'].' = '.(int)$config['rootParentId'] )->limit(1)->commit();
		}
		
		
		
		
		$useQuery = clone $config['query'];
		if( $element === NULL ) {
			// check for rootParentId. If it is set, load children for that element. If its not set, load all elements whose parent is NULL
			$useQuery->where(''.$config['parentIdColumn'].' '.($config['rootParentId']>0 ? ' = '.(int)$config['rootParentId'] : ' <=> NULL'));
		} else {
			// load all given element children.
			$useQuery->where(''.$config['parentIdColumn'].' = '.(int)(is_object($element) ? $element->{$config['idColumn']} : $element[$config['idColumn']]) );
		}
		
		$nodes = array();
		foreach( $useQuery->commit() as $subElement ) {
			$nodes[] = self::makeTree( $config, $subElement, $level+1 );
		}
		
		
		if( !$element )
			return $nodes;
			
		
		$node = DomFgTreeNode::c('');
		$node->set( $nodes );
		if( $config['callback'] ) {
			call_user_func_array($config['callback'], array_merge(array($node,$element),$config['callbackArgs']) );
		} else {
			if( is_object($element) ) {
				$node->name($element->{$config['nameColumn']});
			} else {
				$node->name($element[$config['nameColumn']]);
			}
		}
		return $level == 0 ? array($node) : $node;
		
	}
	
	public function render() {
		$renderer = $this->getRenderer();
		$renderer->assign('tree', $this );
		$renderer->assign('list', implode(' ',$this->source ) );
		return $renderer->fetch('Input/Tree.html');
	}
	

}
?>